/*

File name		: table2.do

Description		: conducts randomization checks by experimental condition

Output File		: table2.tex
				
*/

set more off, perm
capture restore

*** 001 Analysis/Tabulation ***
use "data/blood_donation/blood_donation.dta", clear

	// Merge rainfall data
	merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"
	*, keep(match)

	*rename kalendertag daily_rainfall
	rename daily_rainfall_ZH daily_rainfall
	gen any_rain = 1 if daily_rainfall > 0 & daily_rainfall != .
	replace any_rain = 0 if daily_rainfall == 0
	sort spnr entnahmedatum
	gen any_rain_plus1 = any_rain[_n+1] if spnr == spnr[_n+1]

	sort spnr entnahmedatum
	gen rainfall_plus1 = daily_rainfall[_n+1] if spnr == spnr[_n+1]
	gen strong_rain = 1 if daily_rainfall >= 10
	replace strong_rain = 0 if daily_rainfall < 10

	// Robustnesss: Extreme heat and cold. 
	gen extreme_cold = 1 if daily_high_ZH < 5 & daily_high_ZH != .
	replace extreme_cold = 0 if daily_high_ZH >= 5 & daily_high_ZH != .

	replace daily_low_ZH = "." if daily_low_ZH == "-"
	destring daily_low_ZH, replace
	gen frost_day = 1 if daily_low_ZH < 0 & daily_low_ZH != .
	replace frost_day = 0 if daily_low_ZH >= 0 & daily_low_ZH != .

	gen extreme_heat = 1 if daily_high_ZH > 25 & daily_high_ZH != .
	replace extreme_heat = 0 if daily_high_ZH <= 25 & daily_high_ZH != .

	gen adverse_weather = 0 if extreme_cold != .
	replace adverse_weather = 1 if extreme_cold == 1 | extreme_heat == 1 | strong_rain == 1
	
	// Generate Controls
	gen spring=1 if period==1|period==3
	replace spring=0 if spring==.
	gen month=month(entnahmedatum)
	gen week=week(entnahmedatum)
	tab month, gen(month_dummy)
	tab c_num, gen(c_num_dummy)

	label define order 1 "K" 2 "1a" 3 "1b" 4 "2"
	encode treatment, gen(treatment_ordered) label(order)
	label values treatment_ordered order	
	
	lab var adverse_weather "Adverse Weather"
	lab var phonecall "Call"
	lab var age "Age"
	lab var male "Male"
	lab var bltype_dummy1 "O+ Blood Type"
	lab var bltype_dummy2 "O- Blood Type"
	lab var bltype_dummy3 "A- Blood Type"
	
	keep if period == 1
	
	// Joint F-test
	matrix test=.\.\.\.\.
	local c=0
	foreach i of varlist age male bltype_dummy1 bltype_dummy2 bltype_dummy3 {
		
		local c=`c'+1
		reg `i' i.treatment_ordered if period==1
		
			test 2.treatment_ordered=3.treatment_ordered=4.treatment_ordered=0
			matrix test[`c',1]= round(`r(p)', 0.01)
			
	}
	
	insobs 1400
	
	replace age = test[1,1] if age == .
	replace male = test[2,1] if male == .
	replace bltype_dummy1 = test[3,1] if bltype_dummy1 == .
	replace bltype_dummy2 = test[4,1] if bltype_dummy2 == .
	replace bltype_dummy3 = test[5,1] if bltype_dummy3 == .
	
	replace treatment = "F" if treatment == ""
		
	balancetable (mean if treatment=="1a") (mean if treatment=="1b") ///
	(mean if treatment=="2") (mean if treatment=="K") (mean if treatment=="F") ///
	age male bltype_dummy1 bltype_dummy2 bltype_dummy3 ///
	using "output/tables/table2.tex", ///
	replace ctitles("Condition C1" "Condition C2" "Condition C12" "Control" "F-test (p-val.)") varlab

